package com.shop.seller.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.dao.CustomBrandDao;
import com.shop.domain.PageResult;
import com.shop.mapper.BrandMapper;
import com.shop.mapper.TypeTemplateMapper;
import com.shop.model.Brand;
import com.shop.model.BrandExample;
import com.shop.model.BrandExample.Criteria;
import com.shop.model.TypeTemplate;
import com.shop.seller.service.BrandService;
/**
 * 品牌接口实现类
 * @author Y
 *
 */
@Service("brandService")
public class BrandServiceBean implements BrandService {
	
	@Resource
	BrandMapper brandMapper;
	
	@Resource
	CustomBrandDao customBrandDao;
	@Resource
	TypeTemplateMapper typeTemplateMapper;
	@Override
	public List<Brand> findAll() throws Exception{
		
		BrandExample brandExample = new BrandExample();
		List<Brand> brands = brandMapper.selectByExample(brandExample);
		return brands;
		
	}

	@Override
	public boolean updateById(Brand brand)throws Exception {
		
		int result = brandMapper.updateByPrimaryKey(brand);
		if(result>0){
			return true;
		}
		return false;
	}

	@Override
	public boolean insert(Brand brand) throws Exception{
		int result = brandMapper.insert(brand);
		if(result>0){
			return true;
		}
		return false;
	}

	
	@Override
	public boolean deleteByIds(List<Long> ids)throws Exception {
		BrandExample brandExample = new BrandExample();
		Criteria criteria = brandExample.createCriteria();
		criteria.andIdIn(ids);
		int result = brandMapper.deleteByExample(brandExample);
		if(result>0){
			return true;
		}
		return false;
	}

	@Override
	public List<Brand> findLikeName(String name) throws Exception{  
		name = "%"+name+"%";
		BrandExample brandExample = new BrandExample();
		Criteria criteria = brandExample.createCriteria();
		criteria.andNameLike(name);	
		List<Brand> brands = brandMapper.selectByExample(brandExample);
		return brands;
	}
	@Override
	public List<Brand> findByPage(Map<String, String> map) throws Exception {
		List<Brand> brands = customBrandDao.selectByPage(map);
		return brands;
	}

	@Override
	public PageResult findPage(Integer pageNum, Integer pageSize, String name) throws Exception {
		PageHelper.startPage(pageNum,pageSize);
		if("".equals(name)||null==name){
			Page<Brand> brands = (Page<Brand>) findAll();
			return new PageResult(brands.getTotal(),brands.getResult());
			
		}
		Page<Brand> brands = (Page<Brand>) findLikeName(name);
		return new PageResult(brands.getTotal(),brands.getResult());
	}

	@Override
	public String findBrands(String typeTemplateId) throws Exception {
		Long tempid=Long.parseLong(typeTemplateId);
		TypeTemplate typeTemplate = typeTemplateMapper.selectByPrimaryKey(tempid);
		return typeTemplate.getBrandIds();
	}

	
}
